package com.example.mytestdemo.car

import android.animation.Animator
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.widget.ImageView
import com.airbnb.lottie.LottieAnimationView
import com.airbnb.lottie.LottieProperty
import com.airbnb.lottie.TextDelegate
import com.airbnb.lottie.model.KeyPath
import com.example.mytestdemo.BaseViewBindActivity
import com.example.mytestdemo.R
import com.example.mytestdemo.databinding.ActivityCarLottieBinding

/**
 * @author tgw
 * @date 2022/9/13
 * @describe
 */
class CarLottieActivity : BaseViewBindActivity<ActivityCarLottieBinding>() {
    override fun getLayoutInflateId(): Int {
        return R.layout.activity_car_lottie
    }

    override fun initData() {}
    override fun initUI() {
// 1. lottie_fileName 设定lottie动画所对应的json文件地址。json文件默认需要放置在assets下，
//  设定时不需要再强调assets.

//      2.  如果设定 app:lottie_fileName="other/HamburgerArrow.json "，那么lottie就会读取
//        assets/other/HamburgerArrow.json 。
        mBind.lottie.setAnimation("leke.zip")
        // 因为该json中用到了图片 所以要使用压缩包，
        // 否则报错--You must set an images folder before loading an image. Set it with LottieComposition#setImagesFolder or LottieDrawable#setImagesFolder
//        mBind.lottie.setAnimation("data.json")
        mBind.lottie.repeatCount = 10
//        设定动画初次显示时的进度，类型为float。取值范围0.0 ~ 1.0.
//        mBind.lottie.setProgress(0.8f)
//        设定播放速度，取值类型为float。当速度<1时，动画会慢放，当速度<0时，可以实现倒序播放。
        mBind.lottie.setSpeed(0.5f)
        mBind.lottie.setTextDelegate(TextDelegate(LottieAnimationView(this@CarLottieActivity)).apply {
            setText("sfasf","ttttt")
        })

        val shirt = KeyPath("Shirt", "Group 5", "Fill 1")
        val leftArm = KeyPath("LeftArmWave", "LeftArm", "Group 6", "Fill 1")
        val rightArm = KeyPath("RightArm", "Group 6", "Fill 1")
        mBind.lottie.addValueCallback(shirt, LottieProperty.COLOR) {
            R.color.black
        }

        mBind.lottie.playAnimation()

        mBind.lottie.addAnimatorListener(object : Animator.AnimatorListener{
            override fun onAnimationStart(animation: Animator?) {
                Log.d(TAG, "onAnimationStart: ")
            }

            override fun onAnimationEnd(animation: Animator?) {
                Log.d(TAG, "onAnimationEnd: ")
            }

            override fun onAnimationCancel(animation: Animator?) {
                Log.d(TAG, "onAnimationCancel: ")
            }

            override fun onAnimationRepeat(animation: Animator?) {
                Log.d(TAG, "onAnimationRepeat: ")
            }

        })

        mBind.lottie.addAnimatorUpdateListener {
            Log.d(TAG, "initUI: addAnimatorUpdateListener")
        }
        mBind.lottie.addAnimatorPauseListener(object :Animator.AnimatorPauseListener{
            override fun onAnimationPause(animation: Animator?) {
                Log.d(TAG, "onAnimationPause: ")
            }

            override fun onAnimationResume(animation: Animator?) {
                Log.d(TAG, "onAnimationResume: ")
            }

        })

        val textDelegate = TextDelegate(mBind.lottie1)
        mBind.edit.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                textDelegate.setText("NAME", s.toString())
            }
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int,
                                           after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count:
            Int) {}
        })
        mBind.lottie1.setTextDelegate(textDelegate)

    }
}